package com.zjsystem.zjsy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.zjsystem.zjsy.constant.Constant;
import com.zjsystem.zjsy.dto.ZjxdOrg;
import com.zjsystem.zjsy.mapper.ZjxdOrgMapper;
import com.zjsystem.zjsy.service.ZjxdOrgService;
import com.zjsystem.zjsy.vo.Node;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统组织表 服务实现类
 * </p>
 *
 * @author wyf
 * @since 2024-07-11
 */
@Service
public class ZjxdOrgServiceImpl extends ServiceImpl<ZjxdOrgMapper, ZjxdOrg> implements ZjxdOrgService {
    @Override
    public Node getOrgsTree(List<ZjxdOrg> orgs) {
        Map<String, Node> map = Maps.newHashMap();
        Node root = null;
        for (ZjxdOrg org : orgs) {
            String deptId = org.getDeptId();
            String pid = org.getPid();
            Node node = new Node(deptId, org.getName(), pid);
            map.put(deptId, node);
            if (pid.equals(Constant.min_one)){
                root = node;
            } else {
                Node node1 = map.get(pid);
                if (node1 != null){
                    node1.addChild(node);
                }
            }
        }
        return root;
    }
}
